//
//  TimeTools.m
//  SmartCommunity
//
//  Created by Mac on 15/7/26.
//  Copyright (c) 2015年 smartcommunity. All rights reserved.
//
#define NUMBERSPERIOD @"0123456789."

#import <EventKit/EventKit.h>
#import "TimeTools.h"
#import "UserDefaultsTools.h"

#define MEDICINE_ALERT @"medicine_alert" //闹钟对象的key

@implementation TimeTools
+(BOOL)isEmpty:(NSString *)str
{
    if (str==nil||[str isEqualToStringAdd:@""]) {
        return true;
    }
    return false;
}
#pragma mark 当前时间，年月日时分秒
+(NSString*)getCurrentTimeYMDHMS
{
    NSDate*curTime = [NSDate date];// 获取本地时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];  // 格式化时间NSDate
    NSString*stringFromDate = [formatter stringFromDate:curTime];
    return stringFromDate;
}
/**
 *  当前时间
 *
 *  @return 年月日,返回格式yyyy-MM-dd
 */
+(NSString*)getCurrentTimeYMD
{
    NSDate*curTime = [NSDate date];// 获取本地时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];  // 格式化时间NSDate
    NSString*stringFromDate = [formatter stringFromDate:curTime];
    return stringFromDate;
}



/**
 *  格式化nsdate 为月日
 *
 *  @param date
 *
 *  @return 月日
 */
+(NSString *)getMonthDayWithDate:(NSDate *)date
{
    //    NSDate*curTime = [NSDate date];// 获取本地时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"MM-dd"];  // 格式化时间NSDate
    NSString*stringFromDate = [formatter stringFromDate:date];
    return stringFromDate;
}

/**
 *  格式化nsdate 为时分
 *
 *  @param date
 *
 *  @return 时分
 */
+(NSString *)getHourMinWithDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"HH:mm"];  // 格式化时间NSDate
    NSString*stringFromDate = [formatter stringFromDate:date];
    return stringFromDate;
}
/**
 *  格式化nsdate 为年月日
 *
 *  @param date
 *
 *  @return 年月日
 */
+(NSString *)getYearMonthDayWithDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"yyyy-MM-dd"];  // 格式化时间NSDate
    NSString*stringFromDate = [formatter stringFromDate:date];
    return stringFromDate;
}
/**
 *  格式化date为周几
 *
 *  @param date
 *
 *  @return
 */
+(NSString *)getWeekWithDate:(NSDate *)date
{
    NSString*weekStr=nil;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    //    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [calendar setTimeZone: timeZone];
    
    NSInteger unitFlags = NSYearCalendarUnit |
    NSMonthCalendarUnit |
    NSDayCalendarUnit |
    NSWeekdayCalendarUnit |
    NSHourCalendarUnit |
    NSMinuteCalendarUnit |
    NSSecondCalendarUnit;
    
    NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
    NSInteger week = [comps weekday];
    
    do {
        if (week==1) {
            weekStr=@"日";
            break;
        }
        if (week==2) {
            weekStr=@"一";
            break;
        }
        if (week==3) {
            weekStr=@"二";
            break;
        }
        if (week==4) {
            weekStr=@"三";
            break;
        }
        if (week==5) {
            weekStr=@"四";
            break;
        }
        if (week==6) {
            weekStr=@"五";
            break;
        }
        if (week==7) {
            weekStr=@"六";
            break;
        }
    } while (false);
    
    //    NSLog(@"week is:%@",weekStr);
    return weekStr;
    
}

/**
 *  时间string转date
 *
 *  @param str 必须带小时分钟的格式，yyyy-MM-dd HH:mm
 *
 *  @return
 */
+(NSDate *)getDateWithString:(NSString*)str{
    if ([self isEmpty:str]) {
        return nil;
    }
    //格式校验
    NSString *str1;
    NSArray *dateAndTimeArr = [str componentsSeparatedByString:@" "];
    if (dateAndTimeArr.count==1) {
        str1 = [str stringByAppendingString:@" 00:00:00"];
    }
    else {
        NSString *timeStr = dateAndTimeArr[1];
        NSArray *timeArr = [timeStr componentsSeparatedByString:@":"];
        if (timeArr.count==1) {
            str1 = [str stringByAppendingString:@":00:00"];
        }
        else if (timeArr.count==2){
            str1 = [str stringByAppendingString:@":00"];
        }
        else{
            str1 = str;
        }
    }
    
    //根据字符串转换成一种时间格式 供下面解析
    //    NSString* string = @"2013-07-16 13:21";
    //NSString *str1 = [str stringByReplacingOccurrencesOfString:@"-" withString:@""];
    
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asian/shanghai"];
    [inputFormatter setTimeZone:timeZone]; //setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [inputFormatter dateFromString:str1];
}
/**
 *  +代表将来时间，－代表过去时间
 *
 *  @param year
 *  @param month
 *  @param day
 *  @param hour
 *  @param minute
 *  @param second
 *  @param date
 *
 *  @return nsdate
 */
+(NSDate*)getBeforeYear:(int)year month:(int)month day:(int)day hour:(int)hour minute:(int)minute second:(int)second withDate:(NSDate*)date{
    if (date==nil) {
        return nil;
    }
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSDateComponents *comps = nil;
    
    comps = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:[[NSDate alloc] init]];
    
    [comps setYear:year];
    
    [comps setMonth:month];
    
    [comps setDay:day];
    
    [comps setHour:hour]; //+24表示获取下一天的date，-24表示获取前一天的date；
    [comps setMinute:minute];
    [comps setSecond:second];
    
    NSDate *nowDate = [calendar dateByAddingComponents:comps toDate:date options:0];
    return nowDate;
}

/**
 *  得到nsdate之前或之后多少天的时间
 *
 *  @param days
 *  @param date
 *
 *  @return
 */
+(NSString *)getBefroeDay:(int)days andString:(NSString *)dateStr
{
    NSDate *date = [self getDateWithString:dateStr];
    NSDate *nowdate = [self getBeforeYear:0 month:0 day:days hour:0 minute:0 second:0 withDate:date];
    return [self getYearMonthDayWithDate:nowdate];
}
/**
 *  +代表将来时间，－代表过去时间，天为单位
 *
 *  @param days
 *  @param date
 *
 *  @return nadate
 */
+(NSDate *)getDateBefroeDay:(int)days andDate:(NSDate *)date
{
    
    return [self getBeforeYear:0 month:0 day:days hour:0 minute:0 second:0 withDate:date];;
}

/**
 *  +代表将来时间，－代表过去时间，月为单位
 *
 *  @param months
 *  @param date
 *
 *  @return nadate
 */
+(NSDate *)getDateBefroeMonth:(int)months andDate:(NSDate *)date
{
    
    return [self getBeforeYear:0 month:months day:0 hour:0 minute:0 second:0 withDate:date];
}


//根据2015-01-20格式得到星期
+(NSString *)getWeekString:(NSString *)str
{
    if ([self isEmpty:str]) {
        return @"";
    }
    
    NSDate* date = [self getDateWithString:str];
    return [self getWeekWithDate:date];
}
//根据2015-01-20格式得到01-20
+(NSString *)getMonthDaywithString:(NSString *)str
{
    if ([self isEmpty:str]) {
        return @"";
    }
    
    NSDate* date = [self getDateWithString:str];
    return [self getMonthDayWithDate:date];
}


+(BOOL)checkNumber:(NSString *)string
{
    if ([self isEmpty:string]) {
        return NO;
    }
    NSCharacterSet*cs;
    cs =[[NSCharacterSet characterSetWithCharactersInString:NUMBERSPERIOD]invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
    BOOL basicTest = [string isEqualToStringAdd:filtered];
    return basicTest;
}

+(BOOL)checkMobile:(NSString *)_text
{
    if ([self isEmpty:_text]) {
        return NO;
    }
    NSString *Regex = @"1\\d{10}";
    NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    return [Test evaluateWithObject:_text];
}
/**
 *  格式化证件号码
 *
 *  @param str
 *
 *  @return
 */
+(NSString *)getFormatCardNumWithString:(NSString *)str
{
    if ([self isEmpty:str]) {
        return @"";
    }
    NSMutableString *newStr=[[NSMutableString alloc] init];
    [newStr appendString:str];
    if (str.length==18) {
        [newStr replaceCharactersInRange:NSMakeRange(6, 8) withString:@"****"];
    }
    return newStr;
}


/**
 *  时间间隔
 *
 *  @param dateStr1
 *  @param dateStr2
 *
 *  @return
 */
+ (int)getIntervalSecond:(NSString*)dateStr1 time2:(NSString*)dateStr2{
    
    
    NSDate *time1 = [self getDateWithString:dateStr1];
    NSDate *time2 = [self getDateWithString:dateStr2];
    
    double time = [time2 timeIntervalSinceDate:time1];
    
    return  abs((int)time);
}



+(NSString *)getMonthDayWithInterval:(NSString *)interval
{
    if (interval==nil) {
        return @"";
    }
    //    NSTimeInterval
    
    double inter=[interval doubleValue];
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:inter/1000];
    
    return [self getMonthDayWithDate:date];
}
/**
 *  毫秒转中文月日
 *
 *  @param interval
 *
 *  @return
 */
+(NSString *)getMonthDayChinaWithIntervalUp:(NSString *)interval
{
    if (interval==nil) {
        return @"";
    }
    //    NSTimeInterval
    
    double inter=[interval doubleValue];
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:inter/1000];
    NSString *str=[self getMonthDayWithDate:date];
    NSString *str1=[str stringByReplacingOccurrencesOfString:@"-" withString:@"月"];
    
    return [str1 stringByAppendingString:@"日"];;
}

/**
 *  date转年月日时分
 *
 *  @param date
 *
 *  @return
 */
+(NSString *)getYearMonthDayHourMinWithDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];  // 格式化时间NSDate
    NSString*stringFromDate = [formatter stringFromDate:date];
    return stringFromDate;
}

/**
 *   毫秒转时间－－年月日时分
 *
 *  @param interval 毫秒
 *
 *  @return
 */
+(NSString *)getYearMonthDayHourMinWithInterval:(NSString *)interval
{
    if (interval==nil) {
        return @"";
    }
    //    NSTimeInterval
    
    double inter=[interval doubleValue];
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:inter/1000];
    
    return [self getYearMonthDayHourMinWithDate:date];
}


/**
 *  long 毫秒转时间－－年月日时分
 *
 *  @param interval 毫秒
 *
 *  @return
 */
+(NSString *)getYearMonthDayHourMinWithLongInterval:(long)interval
{
    
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:interval/1000];
    
    return [self getYearMonthDayHourMinWithDate:date];
}
/**
 *  long 毫秒转时间－－年月日
 *
 *  @param interval 毫秒
 *
 *  @return
 */
+(NSString *)getYearMonthDayWithLongInterval:(long)interval
{
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:interval/1000];
    
    return [self getYearMonthDayWithDate:date];
}

/**
 *  date 转英文时间
 *
 *  @param date
 *
 *  @return
 */
+(NSString *)getYearMonthDayHourMinWithDate22:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [formatter setLocale:locale];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"yyyy-MMM-dd"];  // 格式化时间NSDate
    NSString*stringFromDate = [formatter stringFromDate:date];
    return stringFromDate;
}




/**
 *  时间比较  firstStr早于secondestr返回为NO
 *
 *  @param firstStr 第一个时间字符串
 *  @param secondStr 第二个时间字符串
 *
 *  @return BOOL
 */
+(BOOL)firstDateStr:(NSString *)firstStr compareSecondStr:(NSString *)secondStr
{
    if ([self isEmpty:firstStr]||[self isEmpty:secondStr]) {
        return NO;
    }
    
    NSDate* date1 = [self getDateWithString:firstStr];
    NSDate* date2 = [self getDateWithString:secondStr];
    if ([date1 timeIntervalSinceDate:date2]<0) {
        return NO;
    }
    return YES;
}

//返回年月时间
+(NSString*)getYearMonthwithString:(NSString *)str{
    NSDate *date = [self getDateWithString:str];
    return [self getTimeDate:date withFormatter:@"yyyy-MM"];
}

/**
 *  返回传入formatterStr格式时间
 *
 *  @param date 日期
 *  @param formatterStr yyyy-MM-dd yyyy-MM
 *
 *  @return formatterStr格式时间
 */
+(NSString*)getTimeDate:(NSDate*)date withFormatter:(NSString*)formatterStr{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:formatterStr];  // 格式化时间NSDate
    NSString*stringFromDate = [formatter stringFromDate:date];
    return stringFromDate;
}

/**
 *  返回传入formatterStr格式时间
 *
 *  @param timeStr 时间字符串
 *  @param formatterStr yyyy-MM-dd yyyy-MM
 *
 *  @return formatterStr格式时间
 */
+(NSString*)getTimeString:(NSString*)timeStr withFormatter:(NSString*)formatterStr{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:formatterStr];  // 格式化时间NSDate
    NSString*stringFromDate = [formatter stringFromDate:[self getDateWithString:timeStr]];
    return stringFromDate;
}

+ (NSString*)getYearMonthDayWithTimeString:(NSString*)timeStr{
    return [self getTimeString:timeStr withFormatter:@"yyyy-MM-dd"];
}

/**
 *  时间string转date
 *
 *  @param str 时间字符串
 *
 *  @return 字符
 */
+(NSString *)getDateWithStringNew:(NSString*)str{
    if ([self isEmpty:str]) {
        return nil;
    }
    //根据字符串转换成一种时间格式 供下面解析
    //    NSString* string = @"2013-07-16 13:21";
    NSString *str1 = [str stringByReplacingOccurrencesOfString:@"-" withString:@""];
    
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [inputFormatter setTimeZone:timeZone];
    [inputFormatter setDateFormat:@"yyyyMMdd"];
    NSString * str2 = [self getYearMonthDayWithDate:[inputFormatter dateFromString:str1]];
    return str2;
}

//毫秒转中文月日时分
+(NSString *)getMonthDayHourMinWithDate:(NSDate*)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [formatter setLocale:locale];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"MM-dd HH:mm"];  // 格式化时间NSDate
    //    NSDate *date=[NSDate dateWithTimeIntervalSince1970:interval/1000];
    NSString *stringFromDate = [formatter stringFromDate:date];
    
    NSString *str1=[stringFromDate stringByReplacingOccurrencesOfString:@"-" withString:@"月"];
    return [str1 stringByReplacingOccurrencesOfString:@" " withString:@"日 "];
}

//获取当前时间的日
+(NSString*)getCurrentTimeDay
{
    NSDate*curTime = [NSDate date];// 获取本地时间
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:curTime];
    NSString*stringFromDate = [NSString stringWithFormat:@"%ld",(long)[dateComponent day]];
    return stringFromDate;
}

//获取当前时间的月份与星期几
+(NSString*)getCurrentTimeMonthAndWeekDay
{
    NSDate*curTime = [NSDate date];// 获取本地时间
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:curTime];
    NSString*stringFromDate = [NSString stringWithFormat:@"%ld月\n星期%@",(long)[dateComponent month],[self getWeekWithDate:curTime]];
    return stringFromDate;
}

+ (NSString *)getTimeGapHumanDescForDate:(NSString *)dateStr{
    NSDate *today = [NSDate date];
    NSDate *thatday = [self getDateWithString:dateStr];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    
    NSDateComponents *cmps = [calendar components:unit fromDate:thatday toDate:today options:0];
    NSString *desc;
    if (cmps.year==0 && cmps.month==0 && cmps.day==0) {
        desc = @"0天";
    }
    else {
        NSString *year = cmps.year==0?@"":[NSString stringWithFormat:@"%ld年", (long)cmps.year];
        NSString *month = cmps.month==0?@"":[NSString stringWithFormat:@"%ld月", (long)cmps.month];
        NSString *day = cmps.day==0?@"":[NSString stringWithFormat:@"%ld天", (long)cmps.day];
        
        desc = [NSString stringWithFormat:@"%@%@%@", year, month, day];
    }
    
    return desc;
}

+ (EKEvent*)createEventForDate:(NSDate*)date andTitle:(NSString*)title forEventStore:(EKEventStore*)eventDB{
    EKEvent *myEvent  = [EKEvent eventWithEventStore:eventDB]; //创建一个日历事件
    myEvent.title     = title;  //标题
    myEvent.startDate = date; //开始date   required
    myEvent.endDate   = date;  //结束date    required
    EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];
    [myEvent addAlarm:alarm]; //添加一个闹钟  optional
    [myEvent setCalendar:[eventDB defaultCalendarForNewEvents]]; //添加calendar  required
    return myEvent;
}
+ (void)addClockAlert:(NSArray*)dateArr forTitle:(NSString *)title intoDB:(EKEventStore*)eventDB{
    //申请事件类型权限
    [eventDB requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
        if (granted) { //授权是否成功
            NSEnumerator *en = [dateArr objectEnumerator];
            NSDate *date;
            //缓存进本地存储
            NSMutableArray *mt_alerts;
            NSArray *exist = [UserDefaultsTools getUserDefaultsForKey:MEDICINE_ALERT];
            if (exist) {
                mt_alerts = [exist mutableCopy];
            }
            else {
                mt_alerts = [NSMutableArray array];
            }
            while (date = [en nextObject]) {
                EKEvent *myEvent  = [EKEvent eventWithEventStore:eventDB]; //创建一个日历事件
                myEvent.title     = title;  //标题
                myEvent.startDate = date; //开始date   required
                myEvent.endDate   = date;  //结束date    required
                EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];
                [myEvent addAlarm:alarm]; //添加一个闹钟  optional
                [myEvent setCalendar:[eventDB defaultCalendarForNewEvents]]; //添加calendar  required
                NSError *err;
                BOOL ret = [eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err]; //保存
                if (!ret) {
                    debugLog(@"保存日历事件失败！error_msg:%@", err);
                }
                else {
                    NSString *identifier = [myEvent eventIdentifier];
                    [mt_alerts addObject:identifier];
                }
            }
            [UserDefaultsTools saveUserDefaultsObject:mt_alerts forKey:MEDICINE_ALERT];
        }
    }];
}

+ (void)addReminderNotify:(NSDate *)date forTitle:(NSString *)title intoDB:(EKEventStore*)eventDB{
    //申请提醒权限
    [eventDB requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            //创建一个提醒功能
            EKReminder *reminder = [EKReminder reminderWithEventStore:eventDB];
            //标题
            reminder.title = title;
            //添加日历
            [reminder setCalendar:[eventDB defaultCalendarForNewReminders]];
            NSCalendar *cal = [NSCalendar currentCalendar];
            [cal setTimeZone:[NSTimeZone systemTimeZone]];
            NSInteger flags = NSCalendarUnitYear | NSCalendarUnitMonth |
                              NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute |
                              NSCalendarUnitSecond;
            
            NSDateComponents* dateComp = [cal components:flags fromDate:date];
            dateComp.timeZone = [NSTimeZone systemTimeZone];
            reminder.startDateComponents = dateComp; //开始时间
            reminder.dueDateComponents = dateComp; //到期时间
            reminder.priority = 1; //优先级
            EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date]; //添加一个车闹钟
            [reminder addAlarm:alarm];
            NSError *err;
            [eventDB saveReminder:reminder commit:YES error:&err];
            if (err) {
            }
        }
    }];
}

+ (void)removeAllMedicineAlertInDB:(EKEventStore*)eventDB{
    NSArray *exist = [UserDefaultsTools getUserDefaultsForKey:MEDICINE_ALERT];
    if (exist) {
        NSEnumerator *en = [exist objectEnumerator];
        NSString *identifier;
        EKEvent *alert;
        NSError *err;
        while (identifier = [en nextObject]) {
            alert = [eventDB eventWithIdentifier:identifier];
            [eventDB removeEvent:alert span:EKSpanFutureEvents error:&err];
            //debugLog(@"删除闹钟！error_msg:%@", err);
        }
        
        [UserDefaultsTools deleteUserDefaultsForKey:MEDICINE_ALERT];
    }
}

+ (NSDate *)getDateFromUTC:(NSString *)timeOfUTC{
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    [fmt setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [fmt setDateFormat:@"yyyy-MM-dd'T'HH:mm:sszz"];
    NSDate *d = [fmt dateFromString:timeOfUTC];
    return d;
}

@end
